package com.pacifico.telebusca.web.listener;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import com.pacifico.telebusca.web.facade.TeleBuscaFacade;

public class AutenticacionListenerSuccess implements
		AuthenticationSuccessHandler {

	private Logger logger = Logger
			.getLogger(AutenticacionListenerSuccess.class);

	@Autowired
	private TeleBuscaFacade teleBuscaFacade;
	private UserDetails userDetails;

	public void onAuthenticationSuccess(HttpServletRequest arg0,
			HttpServletResponse arg1, Authentication arg2) throws IOException,
			ServletException {

		userDetails = (UserDetails) arg2.getPrincipal();
		teleBuscaFacade.registrarAuditoria("T0022", "T0023");

		arg1.setStatus(HttpServletResponse.SC_OK);
		logger
				.info("Iniciando Session Sistema TeleBusca, Autenticacion satisfactoria "
						+ userDetails.getUsername());
		arg1.sendRedirect(arg0.getContextPath() + "/menu");
	}

}
